package GUI;

import model.*;
import order.*;

import javax.swing.*;

import GUI.MapPanel.TerritoryPanel;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ConvertBackSpyFrame extends OrderFrame{
	 
	 private ConvertBackSpyFrame convertBackSpyFrame;
	 private JComboBox<TechnologyLevel> sourceBox;
	 private ConvertBackSpy temp;

	 public ConvertBackSpyFrame(TextPanel textPanel, TerritoryPanel territoryPanel, Map map){
		super(textPanel, territoryPanel, map);

     	convertBackSpyFrame = this;

		setLayout(new GridLayout(2, 2, 10, 10));	   
		
		this.add(new JLabel("Convert back a spy:  "));	
	  	sourceBox= new JComboBox<TechnologyLevel>();
	  	for (Spy spy : player.getSpyList())
	  		if (spy.getTerritory() == territory)
	  		  sourceBox.addItem(spy.getLevel());
	  	
	  	this.add(sourceBox);
	  	this.add(submitButton);
	  	this.add(cancelButton);
	  	submitButton.addActionListener(new submitListener());
	  	cancelButton.addActionListener(new cancelListener(this));
	  	
	  	this.setSize(400, 150);
	  	this.setVisible(true);
	 }
	 

	 class submitListener implements ActionListener{
   
	  public void actionPerformed(ActionEvent arg0) {
		TechnologyLevel source = (TechnologyLevel)sourceBox.getSelectedItem();
		  for(Spy spy: player.getSpyList())
			  if ((spy.getTerritory() == territory) && (spy.getLevel() == source)){
				  temp = new ConvertBackSpy(spy); 
				  break;
			  }
	
	  	 if (temp.checkOrder()){
		  	    textPanel.addHistory(temp);
		  	    temp.executeOrder();
		  	    textPanel.updatePlayerPanel();
		  	    territoryPanel.update();
		     }
	  	 convertBackSpyFrame.dispose();
	}
	 }
}

